import { BadRequestException, Controller, Get, Inject, Param, Query } from '@nestjs/common';
import { ChatroomService } from './chatroom.service';
import { UserId } from '@/common/decorator/param.decorator';
import { Pagination } from '@/common/dto';
import { GroupListDto } from './dto/chatroom-list.dto';

@Controller('chatroom')
export class ChatroomController {
    @Inject()
    private readonly chatroomService: ChatroomService;

    @Get('create-one-to-one/:friendId')
    oneToOne(@Param('friendId') friendId: number, @UserId() userId: number) {
        return this.chatroomService.createOneToOneChatroom(userId, friendId);
    }

    @Get('group')
    group(@Query('name') name: string, @UserId() userId: number) {
        return this.chatroomService.createGroupChatroom(name, userId);
    }

    @Get('list')
    list(@UserId() userId: number, @Query() groupDto: GroupListDto) {
        return this.chatroomService.list(userId, groupDto);
    }
    @Get('all-list')
    allList(@UserId() userId: number) {
        return this.chatroomService.allList(userId);
    }
    @Get('chatroom-detail/:chatroomId')
    chatroomDetail(@UserId() userId: number, @Param('chatroomId') chatroomId: number) {
        return this.chatroomService.chatroomDetail(userId, chatroomId);
    }

    @Get('members/:chatroomId')
    members(@Param('chatroomId') chatroomId: number) {
        return this.chatroomService.members(chatroomId);
    }

    @Get('info/:chatroomId')
    info(@Param('chatroomId') chatroomId: number) {
        return this.chatroomService.info(chatroomId);
    }

    @Get('join/:chatroomId')
    join(@Param('chatroomId') chatroomId: number, @Query('joinUserId') joinUserId: number) {
        if (!chatroomId) throw new BadRequestException('chatroomId不能为空')
        if (!joinUserId) throw new BadRequestException('joinUserId不能为空')


        return this.chatroomService.join(chatroomId, joinUserId);
    }

    @Get('quit/:chatroomId')
    quit(@Param('chatroomId') chatroomId: number, @Query('quitUserId') quitUserId: number) {
        if (!chatroomId) throw new BadRequestException('chatroomId不能为空')
        if (!quitUserId) throw new BadRequestException('quitUserId')


        return this.chatroomService.quit(chatroomId, quitUserId);
    }
    @Get('findChatroom')
    async findChatroom(@Query('userId1') userId1: string, @Query('userId2') userId2: string) {
        if (!userId1 || !userId2) {
            throw new BadRequestException('用户 id 不能为空');
        }
        return this.chatroomService.queryOneToOneChatroom(+userId1, +userId2);
    }
}
